package top.houry.stack;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/11/18
 **/
public class WaterMark {
    public static void main(String[] args) throws Exception {
//        BufferedImage background = ImageIO.read(new File("/Users/edy/Desktop/63802993958_.jpeg"));
//        Graphics2D bgG2 = (Graphics2D)background.getGraphics();
//
//        //遮罩层大小
//        int coverWidth = background.getWidth();
//        int coverHeight = background.getHeight();
//        //遮罩层位置
//        int coverX = 0;
//        int coverY = background.getHeight() - coverHeight;
//
//        //创建黑色遮罩层
//        BufferedImage cover = new BufferedImage(coverWidth, coverHeight, BufferedImage.TYPE_INT_RGB);
//        Graphics2D coverG2 = (Graphics2D)cover.getGraphics();
//        coverG2.setColor(Color.BLACK);
//        coverG2.fillRect(0,0, coverWidth, coverHeight);
//        coverG2.dispose();
//
//        //开启透明度
//        bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.2f));
//        //描绘
//        bgG2.drawImage(cover, coverX, coverY, coverWidth, coverHeight, null);
//        //结束透明度
//        bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
////        bgG2.dispose();
//
//
//        Font font = new Font("华文仿宋", Font.PLAIN, (int) (background.getWidth() * 0.02));
//
//        //设置画笔颜色
//        Color color = new Color(255,255,255);
//        bgG2.setColor(color);
//        //设置画笔字体属性
//        bgG2.setFont(font);
//        //设置字体坐标
//        int x = background.getWidth() / 30;
//        int y = background.getHeight() / 10;
//
//        //将水印画上
//        bgG2.drawString("2022/10/31" + "  " + "10:07:16", (int) (x + background.getWidth() * 0.01), y);
//        bgG2.drawString("燕赵驾校科二训练场", x, (int) (y + (background.getWidth() * 0.04)));
//        bgG2.drawString("冀AG737学, 17KM/H", x, (int) (background.getHeight() - (background.getHeight() * 0.09)));
//
//        //清空画笔释放资源
//        bgG2.dispose();
//
//
//        //图片保存到本地
//        File file =new File("/Users/edy/Desktop/img664444446.png");
//        ImageIO.write(background, "png", file);


        BufferedImage background = ImageIO.read(new File("/Users/edy/Desktop/63802993958_.jpeg"));
        Graphics2D bgG2 = (Graphics2D)background.getGraphics();

        //遮罩层大小
        int coverWidth = background.getWidth();
        int coverHeight = background.getHeight();
        //遮罩层位置
        int coverX = 0;
        int coverY = background.getHeight() - coverHeight;

        //创建黑色遮罩层
        BufferedImage cover = new BufferedImage(coverWidth, coverHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D coverG2 = (Graphics2D)cover.getGraphics();
        coverG2.setColor(Color.BLACK);
        coverG2.fillRect(0,0, coverWidth, coverHeight);
        coverG2.dispose();

        //开启透明度
        bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.2f));
        //描绘
        bgG2.drawImage(cover, coverX, coverY, coverWidth, coverHeight, null);
        //结束透明度
        bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
//        bgG2.dispose();


        Font font = new Font("华文仿宋", Font.PLAIN, (int) (background.getWidth() * 0.02));

        //设置画笔颜色
        Color color = new Color(255,255,255);
        bgG2.setColor(color);
        //设置画笔字体属性
        bgG2.setFont(font);
        //设置字体坐标
        int x = background.getWidth() / 30;
        int y = background.getHeight() / 10;

        //将水印画上
        bgG2.drawString("2022/10/31" + "  " + "10:07:16", (int) (x + background.getWidth() * 0.01), y);
        bgG2.drawString("燕赵驾校科二训练场", x, (int) (y + (background.getWidth() * 0.04)));
        bgG2.drawString("冀AG737学, 17KM/H", x, (int) (background.getHeight() - (background.getHeight() * 0.09)));



        //清空画笔释放资源
        bgG2.dispose();
        //建立写入文件流
        FileOutputStream out = new FileOutputStream("/Users/edy/Desktop/111.png");
//        File file =new File("/Users/edy/Desktop/img664444446.png");
        //将画布内容写入到目标文件中，格式为PNG
        ImageIO.write(background, "png", out);
        //清空缓冲区
        out.flush();
        //关闭缓冲区
        out.close();


    }



}
